#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMenuBar>
#include <QToolBar>
#include <QStatusBar>
#include <QMessageBox>
#include <QStackedWidget>
#include <QDateEdit>
#include <QLineEdit>
#include <QTableWidget>
#include <QHeaderView>
#include <QCalendarWidget>
#include "checkview.h"
#include "edepartmentchangeview.h"
#include "selectemployeeinfoview.h"
#include "salarydetailview.h"
#include "assessmentdetailview.h"
#include "leaveapplicationview.h"
#include "employeemanager.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_employeeManager(nullptr),
    m_salaryDetailView(nullptr),
    m_assessmentDetailView(nullptr),
    m_leaveApplicationView(nullptr)
{
    // 设置窗口图标
    setWindowIcon(QIcon(":/images/icon.svg"));

    // 创建员工管理器
    m_employeeManager = new EmployeeManager(this);

    setupUI();
    setupMenus();
    setupToolbars();
    setupConnections();

    // 初始显示员工信息查询界面
    onEmployeeInfoQuery();
}

MainWindow::~MainWindow()
{
    delete m_employeeInfoView;
    delete m_checkInOutView;
    delete m_departmentChangeView;
    delete m_salaryDetailView;
    delete m_assessmentDetailView;
    delete m_leaveApplicationView;
    delete m_employeeManager;
    delete ui;
}

void MainWindow::setupUI()
{
    setWindowTitle("员工管理系统 - 员工界面");
    setMinimumSize(1000, 800);

    // 创建中央部件和堆栈窗口
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    // 创建标题栏
    QHBoxLayout *titleLayout = new QHBoxLayout();
    QLabel *titleLabel = new QLabel("员工管理系统 - 员工界面");
    titleLabel->setObjectName("mainTitleLabel");
    QFont titleFont("Microsoft YaHei", 16, QFont::Bold);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLayout->addWidget(titleLabel);

    // 创建堆栈窗口用于切换不同视图
    m_stackedWidget = new QStackedWidget(this);

    // 初始化各个视图
    m_employeeInfoView = new SelectEmployeeInfoView(m_employeeManager, this);
    m_checkInOutView = nullptr; // 稍后在setEmployeeId中初始化
    m_departmentChangeView = nullptr; // 稍后在setEmployeeId中初始化
    m_salaryDetailView = nullptr; // 稍后在setEmployeeId中初始化
    m_assessmentDetailView = nullptr; // 稍后在setEmployeeId中初始化
    m_leaveApplicationView = nullptr; // 稍后在setEmployeeId中初始化

    // 添加视图到堆栈窗口
    m_stackedWidget->addWidget(m_employeeInfoView);
    
    // 为打卡视图添加占位符
    QWidget *checkInOutPlaceholder = new QWidget(this);
    m_stackedWidget->addWidget(checkInOutPlaceholder);
    
    // 为部门调换申请视图添加占位符
    QWidget *deptChangePlaceholder = new QWidget(this);
    m_stackedWidget->addWidget(deptChangePlaceholder);
    
    // 为工资详情视图添加占位符
    QWidget *salaryDetailPlaceholder = new QWidget(this);
    m_stackedWidget->addWidget(salaryDetailPlaceholder);
    
    // 为考核详情视图添加占位符
    QWidget *assessmentDetailPlaceholder = new QWidget(this);
    m_stackedWidget->addWidget(assessmentDetailPlaceholder);
    
    // 为请假申请视图添加占位符
    QWidget *leaveApplicationPlaceholder = new QWidget(this);
    m_stackedWidget->addWidget(leaveApplicationPlaceholder);

    // 将标题栏和堆栈窗口添加到主布局
    mainLayout->addLayout(titleLayout);
    mainLayout->addWidget(m_stackedWidget);

    setCentralWidget(centralWidget);
    statusBar()->showMessage("就绪");
}

void MainWindow::setupMenus()
{
    // 创建菜单栏
    QMenuBar *menuBar = this->menuBar();

    // 文件菜单
    QMenu *fileMenu = menuBar->addMenu("文件");
    fileMenu->addAction("退出", this, &MainWindow::close);

    // 员工菜单
    QMenu *employeeMenu = menuBar->addMenu("员工管理");
    employeeMenu->addAction("查看信息", this, &MainWindow::onEmployeeInfoQuery);
    employeeMenu->addAction("打卡签到", this, &MainWindow::onCheckInOut);
    employeeMenu->addAction("部门调换申请", this, &MainWindow::onDepartmentChangeApply);
    employeeMenu->addAction("工资详情", this, &MainWindow::onSalaryDetail);
    employeeMenu->addAction("考核详情", this, &MainWindow::onAssessmentDetail);
    employeeMenu->addAction("请假申请", this, &MainWindow::onLeaveApplication);

    // 用户菜单
    QMenu *userMenu = menuBar->addMenu("用户");
    userMenu->addAction("退出登录", this, &MainWindow::onLogout);

    // 帮助菜单
    QMenu *helpMenu = menuBar->addMenu("帮助");
    helpMenu->addAction("关于", [this]() {
        QMessageBox::about(this, "关于", "员工管理系统 v1.0\n\n员工界面");
    });
}

void MainWindow::setupToolbars()
{
    // 创建工具栏
    QToolBar *toolBar = addToolBar("工具栏");
    toolBar->setMovable(false);

    // 添加工具按钮
    toolBar->addAction(QIcon(":/images/user_info.svg"), "查看信息", this, &MainWindow::onEmployeeInfoQuery);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/check_in.svg"), "打卡签到", this, &MainWindow::onCheckInOut);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/department_change.svg"), "部门调换申请", this, &MainWindow::onDepartmentChangeApply);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/salary.svg"), "工资详情", this, &MainWindow::onSalaryDetail);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/assessment.svg"), "考核详情", this, &MainWindow::onAssessmentDetail);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/leave.svg"), "请假申请", this, &MainWindow::onLeaveApplication);
    toolBar->addSeparator();
    toolBar->addAction(QIcon(":/images/logout.svg"), "退出登录", this, &MainWindow::onLogout);
}

void MainWindow::setupConnections()
{
    // 可以在这里添加信号槽连接
}

void MainWindow::onEmployeeInfoQuery()
{
    // 切换到员工信息查询视图
    m_stackedWidget->setCurrentWidget(m_employeeInfoView);
    statusBar()->showMessage("查看信息");

    // 设置员工ID并自动查询
    if (!m_employeeId.isEmpty()) {
        m_employeeInfoView->setEmployeeId(m_employeeId);
    }
}

void MainWindow::setEmployeeId(const QString &employeeId)
{
    m_employeeId = employeeId;

    // 初始化打卡视图
    if (m_checkInOutView) {
        delete m_checkInOutView;
        m_checkInOutView = nullptr;
    }
    m_checkInOutView = new CheckView(m_employeeManager, m_employeeId, this);

    // 初始化部门调换申请视图
    if (m_departmentChangeView) {
        delete m_departmentChangeView;
        m_departmentChangeView = nullptr;
    }
    m_departmentChangeView = new EDepartmentChangeView(m_employeeManager, m_employeeId, this);

    // 初始化工资详情视图
    if (m_salaryDetailView) {
        delete m_salaryDetailView;
        m_salaryDetailView = nullptr;
    }
    m_salaryDetailView = new SalaryDetailView(m_employeeManager, m_employeeId, this);

    // 初始化考核详情视图
    if (m_assessmentDetailView) {
        delete m_assessmentDetailView;
        m_assessmentDetailView = nullptr;
    }
    m_assessmentDetailView = new AssessmentDetailView(m_employeeManager, m_employeeId, this);

    // 初始化请假申请视图
    if (m_leaveApplicationView) {
        delete m_leaveApplicationView;
        m_leaveApplicationView = nullptr;
    }
    m_leaveApplicationView = new LeaveApplicationView(m_employeeManager, m_employeeId, this);

    // 更新堆栈窗口中的打卡视图
    // 先移除占位符
    QWidget *placeholder = m_stackedWidget->widget(1);
    if (placeholder) {
        m_stackedWidget->removeWidget(placeholder);
        delete placeholder;
    }
    // 插入新的打卡视图
    m_stackedWidget->insertWidget(1, m_checkInOutView);

    // 更新堆栈窗口中的部门调换申请视图
    // 先移除占位符
    QWidget *deptChangePlaceholder = m_stackedWidget->widget(2);
    if (deptChangePlaceholder) {
        m_stackedWidget->removeWidget(deptChangePlaceholder);
        delete deptChangePlaceholder;
    }
    // 插入新的部门调换申请视图
    m_stackedWidget->insertWidget(2, m_departmentChangeView);

    // 更新堆栈窗口中的工资详情视图
    // 先移除占位符
    QWidget *salaryDetailPlaceholder = m_stackedWidget->widget(3);
    if (salaryDetailPlaceholder) {
        m_stackedWidget->removeWidget(salaryDetailPlaceholder);
        delete salaryDetailPlaceholder;
    }
    // 插入新的工资详情视图
    m_stackedWidget->insertWidget(3, m_salaryDetailView);

    // 更新堆栈窗口中的考核详情视图
    // 先移除占位符
    QWidget *assessmentDetailPlaceholder = m_stackedWidget->widget(4);
    if (assessmentDetailPlaceholder) {
        m_stackedWidget->removeWidget(assessmentDetailPlaceholder);
        delete assessmentDetailPlaceholder;
    }
    // 插入新的考核详情视图
    m_stackedWidget->insertWidget(4, m_assessmentDetailView);

    // 更新堆栈窗口中的请假申请视图
    // 先移除占位符
    QWidget *leaveApplicationPlaceholder = m_stackedWidget->widget(5);
    if (leaveApplicationPlaceholder) {
        m_stackedWidget->removeWidget(leaveApplicationPlaceholder);
        delete leaveApplicationPlaceholder;
    }
    // 插入新的请假申请视图
    m_stackedWidget->insertWidget(5, m_leaveApplicationView);

    // 更新所有需要员工ID的视图
    if (m_stackedWidget->currentWidget() == m_employeeInfoView) {
        m_employeeInfoView->setEmployeeId(m_employeeId);
    }
    // 设置其他视图的员工ID
    m_salaryDetailView->setEmployeeId(m_employeeId);
    m_assessmentDetailView->setEmployeeId(m_employeeId);
    m_leaveApplicationView->setEmployeeId(m_employeeId);
}

void MainWindow::onCheckInOut()
{
    // 切换到打卡签到视图
    m_stackedWidget->setCurrentWidget(m_checkInOutView);
    statusBar()->showMessage("打卡签到");
}

void MainWindow::onDepartmentChangeApply()
{
    // 切换到部门调换申请视图
    m_stackedWidget->setCurrentWidget(m_departmentChangeView);
    statusBar()->showMessage("部门调换申请");
}

void MainWindow::onSalaryDetail()
{
    // 切换到工资详情视图
    m_stackedWidget->setCurrentWidget(m_salaryDetailView);
    statusBar()->showMessage("工资详情");
}

void MainWindow::onAssessmentDetail()
{
    // 切换到考核详情视图
    m_stackedWidget->setCurrentWidget(m_assessmentDetailView);
    statusBar()->showMessage("考核详情");
}

void MainWindow::onLeaveApplication()
{
    // 切换到请假申请视图
    m_stackedWidget->setCurrentWidget(m_leaveApplicationView);
    statusBar()->showMessage("请假申请");
}

void MainWindow::onLogout()
{
    // 退出登录，关闭当前窗口
    close();
}

